package com.ffapp.helper.bean;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.Toast;

import com.clj.fastble.utils.HexUtil;
import com.ffapp.helper.basis.BasisFragment;
import com.ffapp.helper.util.RulerUtil;
import com.leon.lfilepickerlibrary.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;

import my.LogUtil;
import my.StringUtil;

public class ParamListData extends BasisBean {

	//编辑输入类型  无或0默认，普通文本 1普通字符串 2 数字 3浮点数（带小数点的数字）   4正负数
	public static final int INPUT_TYPE_DEFAULT = 1 , INPUT_TYPE_NUM = 2 ,INPUT_TYPE_FLOAT = 3 ,INPUT_TYPE_SIGNED = 4;

	public ParamListData(){

	}

	public ParamListData(ParamListData paramListData, String value) {
		paramListData.setValue(value);
	}

	public ParamListData(String name, String value, String action) {
		this.name = name;
		this.value = value;
		this.action = action;
	}

	public ParamListData(String name, int type, String value, String action) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
	}


	public ParamListData(String name, int type, String value, boolean enable) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.enable = enable;
	}

	public ParamListData(String name, int type, List<String> values, String action) {
		this.name = name;
		this.type = type;
		this.action = action;
		this.values = values;
	}

	public ParamListData(String name, int type, String value, String action, String unit) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
	}

	public ParamListData(String name, int type, String value, String action, String unit,int inputType) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.inputType = inputType;
	}

	public ParamListData(String name, int type, String value, String action, String unit,int min,int max,int inputType) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.min = min;
		this.max = max;
		this.inputType = inputType;
	}

	public ParamListData(String name, int type, String key, List<String> values, String action) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.action = action;
		this.values = values;
	}



	public ParamListData(String name, int type, String key, List<String> values, boolean enable) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.values = values;
		this.enable = enable;
	}

	public ParamListData(String name, int type, String value, String action, String unit, boolean enable) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.enable = enable;
	}

	public ParamListData(String name, int type, String key, List<String> values, String action, String unit) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.action = action;
		this.values = values;
		this.unit = unit;
	}

	public ParamListData(String name, int type, String key, List<String> values, String action, boolean enable) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.action = action;
		this.values = values;
		this.enable = enable;
	}

	public ParamListData(String name, int type, String value, String action, String unit, boolean enable, boolean change) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.enable = enable;
		this.change = change;
	}

	public ParamListData(String name, int type, String value, String action, String unit, boolean enable, boolean change,int inputType) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.enable = enable;
		this.change = change;
		this.inputType = inputType;
	}

	public ParamListData(String name, int type, String value, String action, String unit, boolean enable, boolean change,int min,int max,int inputType) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.action = action;
		this.unit = unit;
		this.enable = enable;
		this.change = change;
		this.min = min;
		this.max = max;
		this.inputType = inputType;
	}

	public ParamListData(String name, int type, String key, List<String> values, String action, String unit, boolean enable) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.action = action;
		this.values = values;
		this.unit = unit;
		this.enable = enable;
	}

	public ParamListData(String name, int type, String value, List<String> values, String action, String key, int isOpen, String unit) {
		this.name = name;
		this.type = type;
		this.value = value;
		this.values = values;
		this.action = action;
		this.key = key;
		this.isOpen = isOpen;
		this.unit = unit;
	}

	public ParamListData(String name, int type, String key, List<String> values, String action, String unit, boolean enable, boolean change) {
		this.name = name;
		this.type = type;
		this.key = key;
		this.action = action;
		this.values = values;
		this.unit = unit;
		this.enable = enable;
		this.change = change;
	}

	private String name;			//要素名称
	private int type;    // 1 = spinner              2 = edit       3  = 开关按钮     4=不同按钮（这里指）    86 = 只是启到分隔的作用
	private String value;			//edittext所需要的值
	private List<String> values;		//spinner所需要的值
	private String action;   //记录AT指令
	private String key;     //蓝牙返回spinner所选择的文字
	private int isOpen;			//0：关，1：开
	private String unit;			//单位


	private boolean enable;			//是否可点击
	private boolean change;            //是否被修改过


	private int min = -88;  //最小值
	private int max = -88;  //最大值
	private int inputType = INPUT_TYPE_DEFAULT ;


	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getValue() {
		return value;
	}

	public List<String> getValues() {
		return values;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
//	public List<String> getValueByType(){
//		List<String> arrlist = new ArrayList<>();
//		switch (getType()){
//			case 1:
//				arrlist.addAll(getValues());
//				break;
//			case 2:
//				arrlist.add(getValue());
//				break;
//		}
//		return arrlist;
//	}

	public int getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(int isOpen) {
		this.isOpen = isOpen;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public boolean getChange() {
		return change;
	}

	public void setChange(boolean change) {
		this.change = change;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getInputtype() {
		return inputType;
	}

	public void setinputType(int inputType) {
		this.inputType = inputType;
	}

	public  int getinputType_REAL(){
		switch (inputType){
			case INPUT_TYPE_DEFAULT:
				return InputType.TYPE_CLASS_TEXT;
			case INPUT_TYPE_NUM:
				return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_NORMAL;
			case INPUT_TYPE_FLOAT:
				return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL;
			case INPUT_TYPE_SIGNED:
				return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED;
		}
		return InputType.TYPE_CLASS_TEXT;
	}

	public boolean isParamsOk(BasisFragment fragment){
		boolean check = true;
		try{
			if(max > 0 && Float.valueOf(value) > max){
				fragment.dismissProgress();
				fragment.showToast(name+"最大值为"+max);
				check = false;
			}

			if(min != -88 && Float.valueOf(value) < min){
				fragment.dismissProgress();
				fragment.showToast(name+"最小值为"+min);
				check = false;
			}

            if ("中心站地址".equals(name)) {
                if (value.length() != 8) {
                    fragment.dismissProgress();
                    fragment.showToast(name+"必须为4个16进制");
                    check = false;
                }
            }


		}catch (Exception e){
			e.printStackTrace();
			check = false;
		}
		return check;
	}

	public String getActionUrl(Context context){
        String url = null;
	    String value = RulerUtil.keyToValue((getValue() == null) ? getKey() : getValue(),getName(),context);
	    if(StringUtils.isEmpty(value)){
	        return null;
        }
        try {
            url =  HexUtil.chineseWordToGbk((getAction() + "=" + value))+"0D0A";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
		return url;
	}

}
